const fs = require("fs");
const path = require("path");

function getImagesInFolder(folderPath) {
  const images = []; // 存放图片路径的数组

  function traverseDirectory(dirPath) {
    const files = fs.readdirSync(dirPath);

    for (let file of files) {
      const fullFilePath = path.join(dirPath, file);

      if (fs.statSync(fullFilePath).isDirectory()) {
        traverseDirectory(fullFilePath); // 如果是子目录则递归调用自身处理该子目录
      } else {
        const extname = path.extname(file);

        if ([".jpg", ".jpeg", ".png"].includes(extname)) {
          images.push(fullFilePath); // 将符合条件的图片路径添加到images数组中
        }
      }
    }
  }

  traverseDirectory(folderPath);

  return images;
}

// 文件夹路径
/** 常用文件夹路径
 * wallPaper壁纸路径 "F:/Program Files/Steam/steamapps/workshop/content"
 *  */
const folderPath = "F:/Program Files/Steam/steamapps/workshop/content";
const images = getImagesInFolder(folderPath);
console.log("图片路径", images);

// 将数组转换为JSON字符串
const jsonString = JSON.stringify(images);

// 同步写入JSON文件
try {
  fs.writeFileSync("images.json", jsonString);
  console.log("Array has been saved to myArray.json");
} catch (err) {
  console.error("An error occurred:", err);
}
